<template>
  <el-dialog
    class="add-progress-model"
    title="里程碑设置"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    :visible="visible"
    :width="$setRem(700)"
    :before-close="handleCancel">
    <div class="form-container">
      <el-form
        :model="ruleForm"
        :rules="rules"
        :label-width="$setRem(200)"
        ref="addProgressForm"
        class="rule-form">
        <el-form-item label="里程碑名称" prop="milepostName" required>
          <el-input placeholder="请输入里程碑名称" v-model="ruleForm.milepostName" clearable></el-input>
        </el-form-item>
        <el-form-item :label="`计划${type === '1' ? '开始' : '完成'}时间`" prop="planTime">
          <el-date-picker
            :value="ruleForm.planTime"
            disabled
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            type="date">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际节点时间" prop="actualTime">
          <el-date-picker
            v-model="ruleForm.actualTime"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="请选择实际节点时间"
            clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入备注"
            v-model="ruleForm.remark"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleCancel">取 消</el-button>
      <el-button
        class="success"
        type="primary"
        :loading="loading"
        @click="handleSubmit">
        提 交
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    loading: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      default: () => ({})
    },
    type: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        milepostName: '',
        planTime: '',
        actualTime: '',
        remark: '',
      },
      // 校验规则
      rules: {
        milepostName: [
          { required: true, message: '请填写里程碑名称' },
          { trigger: 'blur' }
        ]
      }
    };
  },
  watch: {
    visible(val) {
      if (!val) {
        this.ruleForm = {
          milepostName: '',
          planTime: '',
          actualTime: '',
          remark: '',
        };
      }
    },
    formData: {
      deep: true,
      handler(data) {
        if (data) {
          const { planStartTime, planEndTime } = data;
          Object.keys(this.ruleForm).forEach(key => {
            if (key === 'planTime') {
              this.ruleForm[key] = this.type === '1'
                ? planStartTime
                : planEndTime;
              return;
            }
            this.ruleForm[key] = data[key];
          });
        }
      }
    }
  },
  methods: {
    // 取消
    handleCancel() {
      this.$emit('cancel', false);
    },
    handleSubmit() {
      this.$refs.addProgressForm.validate(valid => {
        if (valid) {
          this.$emit('submit', this.ruleForm);
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
  @import '../../../assets/styles/dialog';

  .add-progress-model {
    .form-container {
      /deep/ .el-form-item {
        .el-form-item__label {
          color: #fff;
        }
        .el-form-item__content {
          .el-input, .el-textarea {
            width: 340px;
          }
        }
      }
    }
  }
</style>
